--[[

http://www.giderosmobile.com/forum/discussion/comment/19999#Comment_19999
]]

AnimatedSprite = Core.class(Sprite)

function AnimatedSprite:init(p_sprite, p_frames)
	local pack = TexturePack.new("images/"..p_sprite..".txt", "images/"..p_sprite..".png")
	self.anim = {}
	print(p_frames)
	for iloop=1, p_frames  do
		self.anim[iloop] = Bitmap.new(pack:getTextureRegion(p_sprite.."_"..tostring(iloop)..".png"))
		self.anim[iloop]:setAnchorPoint(0.5,0.5)
	end
	self.frame = 1
	self:addChild(self.anim[1])
	self.nframes = #self.anim
	self.subframe = 0
	self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)  --sets function for every screen refresh
end

function AnimatedSprite:onEnterFrame()  --called every refresh of the screen.
print("subframe="..self.subframe)
	self.subframe = self.subframe + 1  -- increment the call step
	if self.subframe > 4 then			
		self:removeChild(self.anim[self.frame])
		self.frame = self.frame + 1  	--steps through the frames
		if self.frame > self.nframes then  --more than frames then reset
			self.frame = 1
		end
		self:addChild(self.anim[self.frame])
		self.subframe = 0
	end
end


MovieSprite = Core.class(MovieClip)

function MovieSprite:init(p_sprite, p_frames)
	local pack = TexturePack.new("images/"..p_sprite..".txt", "images/"..p_sprite..".png")
	self.anim = {}
	print(p_frames)
	for iloop=1, p_frames  do
		self.anim[iloop] = Bitmap.new(pack:getTextureRegion(p_sprite.."_"..tostring(iloop)..".png"))
		self.anim[iloop]:setAnchorPoint(0.5,0.5)
	end
	self.frame = 1
	self:addChild(self.anim[1])
	self.nframes = #self.anim
	self.subframe = 0
	self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
end
